﻿using AutoMapper;
using DotNetCore.CAP;
using MediatR;
using MyTask.Domain.Commands;
using MyTask.Tasking.Domain.TaskAggregate;
using MyTask.Tasking.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyTask.Tasking.Applicaiton.Commands
{
   public class CreatedTaskInfoCommandHandler : IRequestHandler<CreatedTaskInfoCommand, bool>
    {
        ITaskInfoRepository _taskInfoRepository;
        ICapPublisher _capPublisher;
        IMapper _mapper;
        public CreatedTaskInfoCommandHandler(ITaskInfoRepository taskInfoRepository, ICapPublisher capPublisher, IMapper mapper)
        {
            _taskInfoRepository = taskInfoRepository;
            _capPublisher = capPublisher;
            _mapper = mapper;
        }

        public async Task<bool> Handle(CreatedTaskInfoCommand request, CancellationToken cancellationToken)
        {
            TaskInfo taskInfo = new TaskInfo(request.TaskName, request.StartTime,request.EndTime,request.IsComplete,request.TaskDescrible,request.TaskResult,request.UserInfoId);
            //_mapper.Map(request, taskInfo);
            _taskInfoRepository.Add(taskInfo);
            var result = await _taskInfoRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
            return result;
        }
    }
}
